package cn.edu.lingnan.fruitshop.service.impl;

import cn.edu.lingnan.fruitshop.common.JSONResult;
import cn.edu.lingnan.fruitshop.mapper.GoodstableMapper;
import cn.edu.lingnan.fruitshop.pojo.Carttable;
import cn.edu.lingnan.fruitshop.mapper.CarttableMapper;
import cn.edu.lingnan.fruitshop.pojo.Focustable;
import cn.edu.lingnan.fruitshop.pojo.Goodstable;
import cn.edu.lingnan.fruitshop.pojo.User;
import cn.edu.lingnan.fruitshop.service.CarttableService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张小凡
 * @since 2022-01-04
 */
@Service
public class CarttableServiceImpl extends ServiceImpl<CarttableMapper, Carttable> implements CarttableService {

    @Autowired(required = false)
    private CarttableMapper carttableMapper;

    @Autowired(required = false)
    private GoodstableMapper goodstableMapper;

    /*
    * 加入购物车
    * */
    @Override
    public JSONResult<Void> cartInfo(HttpSession session, Integer goodId,Integer num) {
        if(goodId==null){
            return new JSONResult<>(201,"该商品可能下架了");
        }
        User user =(User) session.getAttribute("user");
        if(user==null){
            return new JSONResult<>(202,"会话超时，需重新登录");
        }

        Integer uid=user.getUserId();

        //根据商品的id和uid查询carttable表
        QueryWrapper<Carttable> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("good_id",goodId).eq("user_id",uid);
        Carttable carttable = carttableMapper.selectOne(queryWrapper);
        if(carttable!=null){
            num = num + carttable.getNum();
            Carttable carttable1 = new Carttable();
            carttable1.setNum(num);
            int update = carttableMapper.update(carttable1,queryWrapper);
            return new JSONResult<>(203,"加入购物车成功，购物车有相同商品");
        }

        //加入收藏  carttable表添加数据
        Carttable carttable2 = new Carttable().setUserId(uid).setGoodId(goodId).setNum(num);
        int row = carttableMapper.insert(carttable2);
        if(row!=1){
            return new JSONResult<>(204,"加入购物车失败，稍后重试");
        }
        return new JSONResult<>(200,"加入购物车成功");
    }

    /*
    * 查看我的购物车信息
    * */
    @Override
    public String myCart(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if(user==null){
            throw new RuntimeException("用户数据不存在");
        }
        Integer uid = user.getUserId();
        QueryWrapper<Carttable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getUserId());
        List<Carttable> carttables = carttableMapper.selectList(queryWrapper);
        model.addAttribute("carttables",carttables);

        QueryWrapper<Goodstable>queryWrapper1 = new QueryWrapper<>();
        List<Goodstable> goodstableList = goodstableMapper.selectList(queryWrapper1);
        model.addAttribute("goodstableList",goodstableList);
        return "user/cart";
    }

    //删除一条购物车记录
    @Override
    public String deleteOneCart(Integer cartId) {
        carttableMapper.deleteById(cartId);
        return "redirect:/carttable/myCart";
    }
}
